package com.ichina.chat.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.ichina.chat.dao.ServerDao;
import com.ichina.chat.daoall.ObjectDao;
import com.ichina.chat.util.MessageListUtil;
import com.ichina.chat.vo.Robot;
import com.ichina.chat.vo.Server;
import com.ichina.chat.vo.Type;
import com.opensymphony.xwork2.ActionSupport;

public class BgAdmin extends ActionSupport implements  ServletRequestAware, ServletResponseAware,ServletContextAware {
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext servletContext;
	@Resource private ObjectDao objectDao;
	private boolean key = false;
	private String result;
	private int pagesize = 5;
	private int pagecount = 1;
	private int cp = 1;
	private String id = "";
	private int count = 0;
	private String hql="";
	private String keyword = "";
	@SuppressWarnings("unchecked")
	public String loadServer(){
		if(key){
			if(request.getParameter("pagesize") != null){
				pagesize = Integer.parseInt(request.getParameter("pagesize"));
			}
			if(request.getParameter("pagecount") != null){
				pagecount = Integer.parseInt(request.getParameter("pagecount"));
			}
			if(request.getParameter("cp") != null){
				cp = Integer.parseInt(request.getParameter("cp"));
			}
			try {
				hql = "from Server";
				int count = objectDao.getAllCounte(hql);
				pagecount = (count + pagesize - 1)/pagesize;
				List<Server> l =  (List<Server>)objectDao.getAll(hql, cp, pagesize);
				//表格开始
				Object[] a = new Object[l.size()];
				int i = 0;
				for(Server ser : l){
					Object[] aq = new Object[]{ser.getSid(),ser.getSname(),ser.getState()};
					a[i] = aq;
					i++;
				}
				response.setContentType("text/html;charset=utf-8");
				String ssss = MessageListUtil.getList(new Object[]{"类型编号","类型名称","状态"}, a,"sid" ,"Server", "c0", cp, pagesize, pagecount, count);
				response.getWriter().print(ssss);
				result = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public String loadRobot(){
		if(key){
			if(request.getParameter("pagesize") != null){
				pagesize = Integer.parseInt(request.getParameter("pagesize"));
			}
			if(request.getParameter("pagecount") != null){
				pagecount = Integer.parseInt(request.getParameter("pagecount"));
			}
			if(request.getParameter("cp") != null){
				cp = Integer.parseInt(request.getParameter("cp"));
			}
			try {
				hql = "from Robot";
				int count = objectDao.getAllCounte(hql);
				pagecount = (count + pagesize - 1)/pagesize;
				List<Robot> l =  (List<Robot>)objectDao.getAll(hql, cp, pagesize);
				//表格开始
				Object[] a = new Object[l.size()];
				int i = 0;
				for(Robot ser : l){
					Object[] aq = new Object[]{ser.getContext(),ser.getType().getTypename(),ser.getTitle(),ser.getCode()};
					a[i] = aq;
					i++;
				}
				response.setContentType("text/html;charset=utf-8");
				String ssss = MessageListUtil.getList(new Object[]{"类容","类型名称","标题","关键字"}, a,"context", "Robot", "c1", cp, pagesize, pagecount, count);
				response.getWriter().print(ssss);
				
				result = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public String loadType(){
		if(key){
			if(request.getParameter("pagesize") != null){
				pagesize = Integer.parseInt(request.getParameter("pagesize"));
			}
			if(request.getParameter("pagecount") != null){
				pagecount = Integer.parseInt(request.getParameter("pagecount"));
			}
			if(request.getParameter("cp") != null){
				cp = Integer.parseInt(request.getParameter("cp"));
			}
			try {
				hql = "from Type";
				int count = objectDao.getAllCounte(hql);
				pagecount = (count + pagesize - 1)/pagesize;
				List<Type> l =  (List<Type>)objectDao.getAll(hql, cp, pagesize);
				//表格开始
				Object[] a = new Object[l.size()];
				int i = 0;
				for(Type ser : l){
					Object[] aq = new Object[]{ser.getTypeid(),ser.getTypename()};
					a[i] = aq;
					i++;
				}
				response.setContentType("text/html;charset=utf-8");
				String ssss = MessageListUtil.getList(new Object[]{"类型编号","类型名称"}, a,"typeid","Type", "c2", cp, pagesize, pagecount, count);
				response.getWriter().print(ssss);
				
				result = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	public String del(){
		response.setContentType("text/html;charset=utf-8");
		if(key){
			try {
			String vo = request.getParameter("vo");
				if(vo != null)
				{
					id = request.getParameter("id");
					if(id != null){
						id = java.net.URLDecoder.decode(id,"UTF-8");
					}
					String c =  request.getParameter("com");
					try{
						Integer.parseInt(id);
						hql = "delete from "+vo+" where "+c+"="+id;
					}catch(Exception e){
						hql = "delete from "+vo+" where "+c+"='"+id+"'";
					}
					objectDao.update(hql);
					response.getWriter().print("删除成功!!!");
				}else{throw new Exception();};
			} catch (Exception e) {
					try {
						response.getWriter().print("删除失败!!!");
						e.printStackTrace();
					} catch (IOException e1) {	}
			}
			result = null;
		}
		return result;
	}
	public String edit(){
		response.setContentType("text/html;charset=utf-8");
		if(key){
			try {
				String vo = request.getParameter("vo");
				if(vo != null)
				{
					String c =  request.getParameter("com");
					id = request.getParameter("id");
					if(id != null){
						id = java.net.URLDecoder.decode(id,"UTF-8");
					}
					hql = "from "+vo+" where "+c+"="+ id;
					Object t = objectDao.getHql(hql);
					if("Type".equals(vo)){
						Type at = (Type)t;
						response.getWriter().print("<div  style='position:relative;' width='210' height='96' class='dragme' name='ftype'>");
						response.getWriter().print("类型编号:"+at.getTypeid());
						response.getWriter().print("<br/>类型名称:<input type='text' value="+at.getTypeid()+" />");
						response.getWriter().print("<button>提交</button>");
						response.getWriter().print("</div>");
					}
					if("Robot".equals(vo)){
						
					}
					if("Server".equals(vo)){
						Server at = (Server)t;
						response.getWriter().print("<div  style='position:relative;' width='210' height='96' class='dragme' name='ftype'>");
						response.getWriter().print("编号:"+at.getSid());
						response.getWriter().print("<br/>名称:<input type='text' value="+at.getSname()+" />");
						response.getWriter().print("<br/>状态:"+at.getState());
						response.getWriter().print("<button>提交</button>");
						response.getWriter().print("</div>");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public void setServletRequest(HttpServletRequest request) {   
        this.request = request;   
    }   
  
    public void setServletResponse(HttpServletResponse response) {   
        this.response = response;   
    }

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public boolean isKey() {
		return key;
	}

	public void setKey(boolean key) {
		this.key = key;
	}
}
